package com.zr.service.finance.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zr.dao.finance.FinanceIncomeDao;
import com.zr.dao.finance.FinanceStatementDao;
import com.zr.dao.finance.FinanceStatementOrderDao;
import com.zr.dao.order.OrderFormDao;
import com.zr.dao.order.OrderStatusDao;
import com.zr.service.finance.FinanceStatementService;
import com.zr.utils.TokenUtil;
import com.zr.vo.finance.FinanceIncome;
import com.zr.vo.finance.FinanceStatement;
import com.zr.vo.order.OrderForm;
import com.zr.vo.order.OrderStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class FinanceStatementServiceImpl implements FinanceStatementService {

    //对账单dao
    @Resource
    private FinanceStatementDao financeStatementDao;

    //对账单及订单中间表dao
    @Resource
    private FinanceStatementOrderDao financeStatementOrderDao;

    //收入dao
    @Resource
    private FinanceIncomeDao financeIncomeDao;

    //订单dao
    @Resource
    private OrderFormDao orderFormDao;

    //订单状态dao
    @Resource
    private OrderStatusDao orderStatusDao;

    //工具类得到当前登录用户
    @Resource
    private TokenUtil tokenUtil;

    /**
     * @author wanglu
     *查询所有对账单
     * @param financeStatement
     * @return
     */
    @Override
    public PageInfo<FinanceStatement> getList(FinanceStatement financeStatement) {
        PageHelper.startPage(financeStatement.getPageNum(), financeStatement.getPageSize());
        PageInfo<FinanceStatement> pageInfo=new PageInfo<FinanceStatement>(financeStatementDao.getList(financeStatement));
        return pageInfo;
    }

    /**
     * @author wanglu
     * 删除对账单
     * @param financeStatement
     * @return
     */
    @Transactional
    @Override
    public int remove(FinanceStatement financeStatement) {
        //恢复原订单状态为已完成
        orderFormDao.rollBackStatus(financeStatement.getStatementId());
        orderStatusDao.rollBackStatus(financeStatement.getStatementId());
        //删除订单中间表
        financeStatementOrderDao.deleteByStatementId(financeStatement.getStatementId());
        //修改收入表
        financeIncomeDao.deleteByStatementId(financeStatement.getStatementId());
        return  financeStatementDao.deleteByPrimaryKey(financeStatement.getId());
    }


    /**
     * @author wanglu
     * 判断是修改还是新增
     * @param financeStatement
     * @return
     */
    @Override
    public int handler(FinanceStatement financeStatement){
       if(financeStatementDao.selectByStatementId(financeStatement)==null){
          return add(financeStatement);
       }else{
          return update(financeStatement);
       }

    }


    /**
     * @author wanglu
     * 新增对账单
     * @param financeStatement
     * @return
     */
    @Transactional
    @Override
    public int add(FinanceStatement financeStatement) {
        //增加订单中间表
        resetRelationOrder(financeStatement);
        //增加收入表
        List<FinanceIncome> incomes=financeStatement.getIncomes();
        if(incomes!=null && incomes.size()!=0){
            for (FinanceIncome income:incomes) {
                income.setStatementId(financeStatement.getStatementId());
             financeIncomeDao.insertSelective(income);
            }
        }
        if(financeStatement.getChecked().equals("是")) financeStatement.setCheckedTime(new Date());

        return financeStatementDao.insertSelective(financeStatement);
    }

    /**
     * @author wanglu
     * 修改对账单
     * @param financeStatement
     * @return
     */
    @Transactional
    @Override
    public int update(FinanceStatement financeStatement) {
        //恢复原订单状态为已完成
        orderFormDao.rollBackStatus(financeStatement.getStatementId());
        orderStatusDao.rollBackStatus(financeStatement.getStatementId());
        //删除订单中间表
        financeStatementOrderDao.deleteByStatementId(financeStatement.getStatementId());
        //修改订单中间表
        resetRelationOrder(financeStatement);
        //修改收入表
        financeIncomeDao.deleteByStatementId(financeStatement.getStatementId());
        List<FinanceIncome> incomes=financeStatement.getIncomes();
        if(incomes!=null && incomes.size()!=0){
            for (FinanceIncome income:incomes) {
                income.setStatementId(financeStatement.getStatementId());
                financeIncomeDao.insertSelective(income);
            }
        }
        if(financeStatement.getChecked().equals("是")){
            financeStatement.setCheckedTime(new Date());
        }
        financeStatement.setModifyTime(new Date());
        financeStatement.setModifyUser(tokenUtil.currentUserName());
        return financeStatementDao.updateByStatementIdSelective(financeStatement);
    }

    /**
     * @author wanglu
     * 设置对账单中间订单表状态
     * @param status
     * @param orderForm
     * @return
     */
    private int setCurrentStatus(String status,OrderForm orderForm){
        orderForm.setCurrentStatus(status);
        if(status.equals("已结算")){
            orderForm.setCurrentStatusTime(new Date());
        }
        orderFormDao.setStatus(orderForm);
        OrderStatus orderStatus=new OrderStatus();
        orderStatus.setStatusOrderId(orderForm.getOrderId());
        orderStatus.setStatusName(status);
        orderStatus.setStatusTime(new Date());
       return orderStatusDao.insertSelective(orderStatus);
    }

    /**
     * @author wanglu
     * 设置关联订单状态
     * @param financeStatement
     * @return
     */
    private int resetRelationOrder(FinanceStatement financeStatement){
        List<OrderForm> orderList=financeStatement.getRelationOrders();
        if(orderList!=null && orderList.size()!=0){
            for (OrderForm orderForm:orderList) {
                financeStatementOrderDao.add(financeStatement.getStatementId(),orderForm.getOrderId());
                //设置订单状态为结算中或已结算
                setCurrentStatus(financeStatement.getChecked().equals("是")?"已结算":"结算中",orderForm);
            }
        }
        return 1;
    }

}
